package com.jrx.anytxn.transaction.job.customer.config;

import com.jrx.anytxn.common.constant.Constants;
import com.jrx.anytxn.customer.entity.CmCustomerExtInfo;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.entity.PrProductTable;
import com.jrx.anytxn.param.service.product.IProductTableService;
import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.job.customer.step.ActiveProductProcessor;
import com.jrx.anytxn.transaction.job.customer.step.ActiveProductWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @comment: 产品激活
 * @author: liuzb
 * @date: 2023/05/03 10:00
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
@EnableBatchProcessing
public class ProductActiveJobConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    AnytxnBatchProperties anytxnBatchProperties;
    @Autowired
    ActiveProductProcessor activeProductProcessor;
    @Autowired
    ActiveProductWriter activeProductWriter;
    @Autowired
    IProductTableService productTableService;
    @Autowired
    PlatformTransactionManager platformTransactionManager;

    @Bean
    public Job productActiveJob() {
        return jobBuilderFactory.get("productActiveJob")
                .incrementer(new RunIdIncrementer())
                .start(activeProductStep())
                .build();
    }

    @Bean
    public Step activeProductStep() {
        return stepBuilderFactory.get("activeProductStep")
                .<CmCustomerExtInfo, CmCustomerExtInfo>chunk(anytxnBatchProperties.getDefaultConfig().getChunkLimit())
                .reader(notActiveCustomerReader())
                .processor(activeProductProcessor)
                .writer(activeProductWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getDefaultConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<CmCustomerExtInfo> notActiveCustomerReader() {
        MyBatisCursorItemReader<CmCustomerExtInfo> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        cursorItemReader.setQueryId("com.jrx.anytxn.customer.mapper.ext.ExtCmCustomerExtInfoMapper.queryNotActiveCustomers");
        //查询参数设置
        Map<String, Object> map = new HashMap<>();
        map.put("tenantId", Constants.DEFAULT_TENANT_ID);
        map.put("productActiveFlag", Constant.NO_FLAG);
        List<PrProductTable> productTableList = productTableService.findAllForBatch(Constant.STATUS_1);
        List<String> productIds = productTableList.stream().map(PrProductTable::getProductId).collect(Collectors.toList());
        map.put("productIds", productIds);
        cursorItemReader.setParameterValues(map);
        SynchronizedItemStreamReader<CmCustomerExtInfo> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }

}
